package com.maker.generator.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dwork.base.controller.BaseController;
import com.dwork.base.controller.entity.BaseResultObject;
import com.dwork.base.pagination.Page;
import com.dwork.base.pagination.Pagination;
import com.dwork.base.validate.groups.DEFAULT;
import com.maker.generator.entity.MetaGenAttr;
import com.maker.generator.entity.MetaGenObj;
import com.maker.generator.service.IGenService;
@Controller
@RequestMapping(value = "metaGenAttr", name = "元属性管理")
public class MetaGenAttrController extends BaseController{
	private static Logger logger = LoggerFactory.getLogger(MetaGenAttrController.class);
	@Autowired
	private IGenService genService;

	@RequestMapping(value = "/queryObjAttrs", name = "查询单个元对象的属性")
	@ResponseBody
	public void queryAttrs(Integer pageNum,Integer pageSize ,Long objId, String attrName, String attrComment) {
		BaseResultObject baseResultObject = new BaseResultObject();
				
		Map<String, Object> paramMap = new HashMap<String,Object>();
		Page page = new Page();
		page.setCurrentPage(pageSize);
		paramMap.put("metaGenObjId", objId);
		paramMap.put("limit", (pageSize+1)*pageNum);
		paramMap.put("offset", pageSize*pageNum);
		if(attrName!=null&&!attrName.equals("")){
			paramMap.put("attrNameLike", attrName);
		}
		if(attrComment!=null&&!attrComment.equals("")){
			paramMap.put("attrCommentLike", attrComment);
		}
		Map<String, Object> map = new HashMap<String, Object>();
		Pagination<MetaGenAttr> metaGenAttrPage = genService.queryMetaGenAttrList(page, paramMap);
		if(metaGenAttrPage!=null&&metaGenAttrPage.getData()!=null){
			map.put("rows", metaGenAttrPage.getData());
			map.put("pageSize", metaGenAttrPage.getData().size());
			map.put("total", metaGenAttrPage.getPage().getTotalCount());
		}
		baseResultObject.setData(map);
		this.putInToJsonResponse(baseResultObject);
	}
	@RequestMapping(value = "/add", name = "添加元属性")
	@ResponseBody
	public void add(@RequestBody MetaGenAttr view) {
		BaseResultObject baseResultObject = new BaseResultObject();
		List<Map<String, String>> validateMessages = new ArrayList<Map<String, String>>();
		baseResultObject.setStatus(BaseController.STATUS_SUCCESS);
		baseResultObject.setMessage(BaseController.SUCCESS_MESSAGE);
		
		validateMessages = getValidatorMessage(view, DEFAULT.class);
		if(validateMessages!=null&&validateMessages.size()>0) {
			baseResultObject.setStatus(BaseController.STATUS_FAILURE);
			baseResultObject.setMessage(BaseController.VALIDATE_ERROR_MESSAGE);
			baseResultObject.setValidateMessages(validateMessages);
		}else{
			try{
				view = this.genService.save(view);
			}catch (Exception e) {
				logger.debug("后台错误:"+e.getMessage());
				baseResultObject.setStatus(BaseController.STATUS_FAILURE);
				baseResultObject.setMessage(BaseController.COMMON_ERROR_MESSAGE);
			}
			baseResultObject.setData(view);
		}
		putInToJsonResponse(baseResultObject);
	}
	@RequestMapping(value = "/edit", name = "编辑元属性")
	@ResponseBody
	public void edit(@RequestBody MetaGenAttr view) {
		BaseResultObject baseResultObject = new BaseResultObject();
		List<Map<String, String>> validateMessages = new ArrayList<Map<String, String>>();
		baseResultObject.setStatus(BaseController.STATUS_SUCCESS);
		baseResultObject.setMessage(BaseController.SUCCESS_MESSAGE);
		MetaGenAttr entity = this.genService.findMetaGenAttrById(view.getId());
		entity.setAttrName(view.getAttrName());
		entity.setAttrComment(view.getAttrComment());
		entity.setAttrShortTypeName(view.getAttrShortTypeName());
		entity.setAttrType(view.getAttrType());
		entity.setIsBaseType(view.getIsBaseType());
		entity.setRelationShip(view.getRelationShip());
		entity.setRelationType(view.getRelationType());
		
		validateMessages = getValidatorMessage(entity, DEFAULT.class);
		if(validateMessages!=null&&validateMessages.size()>0) {
			baseResultObject.setStatus(BaseController.STATUS_FAILURE);
			baseResultObject.setMessage(BaseController.VALIDATE_ERROR_MESSAGE);
			baseResultObject.setValidateMessages(validateMessages);
		}else{
			try{
				view = this.genService.update(entity);
			}catch (Exception e) {
				logger.debug("后台错误:"+e.getMessage());
				baseResultObject.setStatus(BaseController.STATUS_FAILURE);
				baseResultObject.setMessage(BaseController.COMMON_ERROR_MESSAGE);
			}
			baseResultObject.setData(view);
		}
		putInToJsonResponse(baseResultObject);
	}
	@RequestMapping(value = "/delete", name = "删除元属性")
	@ResponseBody
	public void delete(@RequestBody List<Long> ids) {
		BaseResultObject baseResultObject = new BaseResultObject();
		baseResultObject.setStatus(BaseController.STATUS_SUCCESS);
		baseResultObject.setMessage(BaseController.SUCCESS_MESSAGE);
		try{
			this.genService.deleteMetaGenAttrByIds(ids);
		}catch (Exception e) {
			logger.debug("后台错误:"+e.getMessage());
			baseResultObject.setStatus(BaseController.STATUS_FAILURE);
			baseResultObject.setMessage(BaseController.COMMON_ERROR_MESSAGE);
		}
			
		putInToJsonResponse(baseResultObject);
	}
	@RequestMapping(value = "/g2Edit/{id}", name = "进入元属性修改界面")
	public ModelAndView g2Edit(@PathVariable Long id) {
		ModelAndView mv = getBaseMV("/metaGenAttr/editForm");
		if(id!=null){
			MetaGenAttr entity = this.genService.findMetaGenAttrById(id);
			mv.addObject("entity", entity);
		}
		return mv;
	}
	@RequestMapping(value = "/g2Add", name = "进入元属性新增界面")
	public ModelAndView g2Add(Long objId) {
		
		ModelAndView mv = getBaseMV("/metaGenAttr/addForm");
		mv.addObject("objId", objId);
		return mv;
	}
}
